package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    DishMapper dishMapper;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        //创建表数据对象
        Setmeal setmeal = new Setmeal();
        //把前端发送的数据拷贝进数据库对象
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //插入套餐数据
        setmealMapper.insert(setmeal);
        //获取套餐id
        Long setmealId = setmeal.getId();
        //获取菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //判断有没有菜品
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            setmealDishMapper.insertBatch(setmealDishes);
        }

    }

    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //设置分页参数
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        //调用方法,查询数据分页
        Page<SetmealVO> pageQuery = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(pageQuery.getTotal(), pageQuery.getResult());

    }

    /**
     * 套餐起售停售
     *
     * @param status
     * @param id
     */
    @Override
    @Transactional
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        //如果套餐里的菜品是禁售状态,套餐不能启售
        List<SetmealDish> setmealDishList =setmealDishMapper.getBySetmealId(id);
        for (SetmealDish setmealDish : setmealDishList) {
            Long dishId = setmealDish.getDishId();
            Dish dish=dishMapper.getById(dishId);
            if (dish.getStatus() == StatusConstant.DISABLE){
             throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        setmealMapper.update(setmeal);

    }

    /**
     * 批量删除数据
     *
     * @param ids
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        //遍历ids集合拿到其中的每一个id
//        ids.forEach(id -> {
//
//        });
        for (Long id : ids) {
            //根据id查找数据,封装成一个对象
            Setmeal setmeal = setmealMapper.getById(id);
            //如果当前套餐正在售卖就不能删除
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        //批量删除套餐数据
        for (Long id : ids) {
            //删除套餐数据
            setmealMapper.deleteById(id);
            //删除套餐对应的关系表数据
            setmealDishMapper.deleteBySetmeal(id);
        }
    }

    /**
     * 根据id查询回显数据
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO selectById(Long id) {
        //查询套餐数据
        Setmeal setmeal = setmealMapper.getById(id);
        //根据套餐id查取对应的菜品数据
        List<SetmealDish> setmealDishList = setmealDishMapper.getBySetmealId(id);
        //创建VO对象
        SetmealVO setmealVO = SetmealVO.builder()
                .setmealDishes(setmealDishList)
                .build();
//        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
//        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void updateWithDish(SetmealDTO setmealDTO) {
        //创建实体类对象
        Setmeal setmeal = new Setmeal();
        //把dto里的数据拷贝进实体类
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //先修改套餐数据
        setmealMapper.update(setmeal);
        //删除原有关联套餐菜品数据
        setmealDishMapper.deleteBySetmeal(setmeal.getId());
        //获取菜品数据
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        //判断菜品数据集合里是否有数据
        if (setmealDishList != null && setmealDishList.size() > 0) {
            setmealDishList.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
                setmealDishMapper.insertBatch(setmealDishList);
        }
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
